package com.ksd.control;

import com.alibaba.fastjson.JSONObject;
import com.ksd.control.result.BaseResult;
import com.ksd.control.result.ResultParam;
import com.ksd.dao.IWorklogDAO;
import com.ksd.pojo.Worklog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller()
public class WorklogAction extends BaseAction implements CRUB {
    @Autowired
    private IWorklogDAO worklogDAO;

    private List worklogList;
    private String listParam;
    private String update;
    private String delete;
    private String insert;

    public List getWorklogList() {
        return worklogList;
    }

    public void setWorklogList(List worklogList) {
        this.worklogList = worklogList;
    }

    public String getListParam() {
        return listParam;
    }

    public void setListParam(String listParam) {
        this.listParam = listParam;
    }

    public String getUpdate() {
        return update;
    }

    public void setUpdate(String update) {
        this.update = update;
    }

    public String getDelete() {
        return delete;
    }

    public void setDelete(String delete) {
        this.delete = delete;
    }

    public String getInsert() {
        return insert;
    }

    public void setInsert(String insert) {
        this.insert = insert;
    }

    @Override
    public String list() {
        JSONObject jsonObject = (JSONObject) JSONObject.parse(listParam);
        worklogList = worklogDAO.findAllByUsername(jsonObject.getString("username"));
        setResult(new BaseResult().setResult(ResultParam.SUCCESS));
        return SUCCESS;
    }

    @Override
    public String update() {
        Worklog worklog = JSONObject.parseObject(update, Worklog.class);
        worklogDAO.update(worklog);
        return SUCCESS;
    }

    @Override
    public String delete() {
        Worklog worklog = JSONObject.parseObject(delete, Worklog.class);
        worklogDAO.delete(String.valueOf(worklog.getId()));
        return SUCCESS;
    }

    @Override
    public String insert() {
        Worklog worklog = JSONObject.parseObject(insert, Worklog.class);
        worklogDAO.insert(worklog);
        return SUCCESS;
    }
}
